import {
    Entity,
    Column, Tree,
    TreeParent,
    TreeChildren,
    RelationId,
    ManyToOne,
    OneToMany,
    JoinColumn,
    AfterUpdate,
    AfterInsert
} from 'typeorm';
import { YhBaseEntity } from '@/common/baseentity';
import { SysRoleMenuAction, SysRoleMenu, SysRoleUser } from '@/auth/entities/auth.entity';
import { ApiProperty } from '@nestjs/swagger';
export enum UserStatusEnum {
    inuse = '在用',
    suspend = '停用',
}

@Entity({
    name: 'sys_user',
})
export class SysUser extends YhBaseEntity {
    @Column({
        type: 'varchar',
        name: 'name',
        comment: '姓名',
        unique: true,
    })
    name: string;

    @Column({
        type: 'varchar',
        name: 'pass',
        comment: '密码',
    })
    pass: string;

    @Column({
        type: 'enum',
        enum: UserStatusEnum,
        name: 'status',
        comment: '账号状态',
    })
    status: string;

    @Column({
        type: 'int',
        name: 'cfs_id',
        comment: '站点ID'
    })
    cfs_id: number;

    @Column({
        type: 'datetime',
        name: 'last_time',
        comment: '最后登录时间',
    })
    last_time: Date;
    
    @Column({
        type: 'int',
        name: 'login_err_num',
        comment: '错误登录次数',
    })
    login_err_num: number;

    @Column({
        type: 'datetime',
        name: 'login_err_time',
        comment: '错误登录时间',
    })
    login_err_time: Date;

    @OneToMany(() => SysRoleUser, (roleuser) => roleuser.user)
    roleusers: SysRoleUser[];
}

export enum MenuType {
    Menu = 'menu',
    MenuItem = 'menuitem',
}

@Entity({
    name: 'sys_menu',
})
@Tree('materialized-path')
export class SysMenu extends YhBaseEntity {
    @Column({
        type: 'varchar',
        length: 255,
        name: 'label',
        comment: '菜单名称',
    })
    label: string;

    @Column({
        type: 'varchar',
        length: 50,
        name: 'component_name',
        comment: 'VUE端组件名称',
    })
    component_name: string;

    @Column({
        type: 'enum',
        enum: MenuType,
        name: 'type',
        comment: '菜单类型',
    })
    type: MenuType;

    @Column({
        type: 'int',
        name: 'orderno',
        comment: '排序',
    })
    orderno: number;

    @Column({
        type: 'varchar',
        length: 255,
        name: 'component_path',
        comment: 'VUE端引入compoent路径',
    })
    component_path: string;

    @Column({
        type: 'varchar',
        length: 255,
        name: 'component_icon',
        comment: '菜单图表 element plus el-icon'
    })
    component_icon: string;
    /* @Column({
        type: 'int',
        name: 'parentId',
        comment: '父ID',
    })
    parentId: number; */

    @TreeChildren()
    children: SysMenu[];

    @TreeParent()
    parent: SysMenu;

    @RelationId((menu: SysMenu) => menu.parent)
    parentId: number;

    @OneToMany(() => SysMenuAction, (menuaction) => menuaction.menu)
    menuactions: SysMenuAction[];

    @OneToMany(() => SysRoleMenu, (rolemenu) => rolemenu.menu)
    rolemenus: SysRoleMenu[];
}

@Entity({
    name: 'sys_action',
})
export class SysAction extends YhBaseEntity {
    @Column({
        type: 'varchar',
        name: 'label',
        length: 255,
        comment: 'Action名称',
    })
    label: string;

    @Column({
        type: 'varchar',
        name: 'url',
        length: 255,
        comment: '后端URL',
    })
    url: string;

    @OneToMany(() => SysMenuAction, (menuaction) => menuaction.action)
    menuactions: SysMenuAction[];

    @OneToMany(() => SysRoleMenuAction, (roleaction) => roleaction.action)
    roleactions: SysRoleMenuAction[];

    /* @AfterInsert()
    updateCounters() {
        console.log('after insert action');
    }

    @AfterUpdate()
    update1Counters() {
        console.log('after update action');
    } */
}

@Entity({
    name: 'sys_menuaction',
})
export class SysMenuAction extends YhBaseEntity {
    @ManyToOne(() => SysMenu, (menu) => menu.menuactions)
    @JoinColumn({
        name: 'menuId',
    })
    menu: SysMenu;

    /* @RelationId((menuaction: SysMenuAction) => menuaction.menu)
    menuId: number; */

    @ManyToOne(() => SysAction, (action) => action.menuactions)
    @JoinColumn({
        name: 'actionId',
    })
    action: SysAction;

    /* @RelationId((menuaction: SysMenuAction) => menuaction.action)
    actionId: number; */

    @Column({
        type: 'tinyint',
        name: 'necessary',
        comment: '必需功能'
    })
    necessary: boolean;
}

@Entity({
    name: 'sys_role',
})
export class SysRole extends YhBaseEntity {
    @Column({
        type: 'varchar',
        name: 'name',
        length: 255,
        comment: '角色名称',
    })
    name: string;
}

